package com.easybuilder.core.schedultask.job;

import com.easybuilder.blog.service.ArticleService;
import com.easybuilder.blog.service.CommentService;
import com.easybuilder.blog.service.ReplyService;
import com.easybuilder.common.enums.DingRobotEnum;
import com.easybuilder.common.utils.DingUtil;
import com.easybuilder.core.rbac.service.SysUserService;
import com.easybuilder.core.seo.entity.SysSeoHistory;
import com.easybuilder.core.seo.service.SeoHistoryService;
import org.apache.tomcat.util.codec.binary.Base64;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.List;

@Component
public class SystemReportJob implements Job {

    @Resource
    private SysUserService userService;
    @Resource
    private SeoHistoryService seoHistoryService;
    @Resource
    private ArticleService articleService;
    @Resource
    private CommentService commentService;
    @Resource
    private ReplyService replyService;



    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 1.统计今日用户注册量
        List<Integer> userCount = userService.countUser();
        // 2.统计今日seo引擎抓取次数
        List<Integer> seoCount = seoHistoryService.countHistory();
        // 3.统计今日文章发布数量
        List<Integer> articleCount = articleService.countArticle();
        // 4.统计今日新增评论数量
        List<Integer> commentCount = commentService.countComment();
        // 5.统计回复量
        List<Integer> replyCount = replyService.countReply();
        // 构建消息体
        String message = buildMessage(userCount, seoCount, articleCount, commentCount, replyCount);
        // 发送消息
        sendDingMessage(message);

    }

    /** 构建消息 */
    private String buildMessage(List<Integer> userCount, List<Integer> seoCount, List<Integer> articleCount, List<Integer> commentCount, List<Integer> replyCount) {
        StringBuilder message = new StringBuilder("《主人，今日系统报告出炉！》\n");
        message.append("------------用户统计----------\n");
        message.append("    用户总量：").append(userCount.get(0)).append("\n    ").append("今日新增：").append(userCount.get(1)).append("\n");

        message.append("------------文章统计----------\n");
        message.append("    文章总量：").append(articleCount.get(0)).append("\n    ").append("今日新增：").append(articleCount.get(1)).append("\n");

        message.append("------------评论统计----------\n");
        message.append("    评论总量：").append(commentCount.get(0)).append("\n    ").append("今日新增：").append(commentCount.get(1)).append("\n");

        message.append("------------回复统计----------\n");
        message.append("    回复总量：").append(replyCount.get(0)).append("\n    ").append("今日新增：").append(replyCount.get(1)).append("\n");

        message.append("------------seo统计----------\n");
        message.append("    抓取总量：").append(seoCount.get(0)).append("\n    ").append("今日抓取：").append(seoCount.get(1)).append("\n");

        return message.toString();
    }

    /** 发送钉钉消息 */
    public void sendDingMessage(String message) {
        String url = DingRobotEnum.XTBG.getValue();
        Long timestamp = System.currentTimeMillis();
        String secret = "SECb69ce017165ae47528fa375cae50e13a6bd389b2cbf0f86b2b82f77d804ad358";
        try {
            String stringToSign = timestamp + "\n" + secret;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
            System.out.println(sign);
            url += "&timestamp="+timestamp+"&sign="+sign;
            DingUtil.sendRobotMsg(url, message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
